{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}

    <!--Treeview-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='plugins/bootstrap-treeview/css/bootstrap-treeview.min.css') }}">
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">

{% endblock %}


<!-- Content head -->
{% block head_title %}
    代码发布
{% endblock %}

{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">代码发布</a></li>
        <li class="active">项目管理</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    项目授权
{% endblock %}

{% block body_form %}
    <form name="permissionForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">基本信息</p>
            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>项目名称</label>
                            <div class="col-lg-8">
                                <select class="select2 form-control" name="project_id" data-placeholder="请选择项目名称" data-rule="项目名称: required;">
                                    <option value="{{ id }}"  selected>{{ name }}</option>
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>授权用户</label>
                            <div class="col-lg-8">
                                <select class="select2 form-control" name="user_ids" data-placeholder="请选择授权用户" data-rule="授权用户: required;">
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>

        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>
    <!--Treeview-->
    <script src="{{ url_for('static', filename='plugins/bootstrap-treeview/js/bootstrap-treeview.min.js') }}"></script>

    <script type="text/javascript">

        $(function () {
            bees.initSelect2({
            });

            bees.initServerSideSelect2(
                $('select[name="user_ids"]'),
                {url: '{{ url_for('users.api-user-list') }}', selected: '{{ user_ids }}'},
                {multiple: true}
            );

            var $formEle = $('form[name="permissionForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                var formData = bees.serializeToObject($formEle.serializeArray());
                console.log(formData);
                $.ajax({
                    type: 'post',
                    url: '{{ url_for("codes.project-permission", pk=default_pk) }}'.replace(bees.defaultPK, '{{ id }}'),
                    data : formData,
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '授权成功'
                            });
                            window.location.href = '{{ url_for("codes.project-list") }}'
                        }
                    }
                });
            })
        })

    </script>

{% endblock %}
